<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 组织架构内容  头部 -->
      <el-card class="tree-card">
        <!-- 放置结构内容 -->
        <TreeTools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <!-- 插槽内容 -->
          <TreeTools slot-scope="{data}" :tree-node="data" @addDepts="addDepts" @delDepts="getDepartments" @editDepts="editDepts" />
        </el-tree>
      </el-card>
      <!--  /组织架构内容  头部-->
    </div>
    <!-- 放置新增弹层组件 -->
    <AddDept ref="getinfo" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="addDepts" @addDepartments="getDepartments" />
  </div>
</template>

<script>
import TreeTools from './components/tree-tooles.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeDate } from '@/utils'
import AddDept from './components/add-dept.vue'
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      company: { name: '', manager: '负责人', id: '' }, // 头不数据结构
      departs: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false,
      node: null
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments()
      this.company.name = result.companyName
      this.departs = tranListToTreeDate(result.depts, '') // 需要转化成树形结构
      // console.log(result)
    },
    addDepts(node) {
      this.showDialog = true
      this.node = node // 记录当前点击的node节点
    },
    editDepts(node) {
      this.showDialog = true
      this.node = node
      this.$refs.getinfo.getDepartmentDetail(node.id)

      // console.log(this.editObj)
    }
  }

}
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
